package com.kust.eduService.controller.front;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kust.commomUtils.JwtUtils;
import com.kust.commomUtils.Result;
import com.kust.eduService.client.OrderClient;
import com.kust.eduService.entity.Course;
import com.kust.eduService.entity.chapter.ChapterVo;
import com.kust.eduService.entity.frontvo.CourseFrontVo;
import com.kust.eduService.entity.frontvo.CourseWebVo;
import com.kust.eduService.service.EduChapterService;
import com.kust.eduService.service.EduCourseService;
import com.kust.serviceBase.orderVo.CourseWebVoOrder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author 马强
 * @Description
 * @create 2022-07-01 15:50
 */
@Api(tags = "前端课程管理")
@RestController
@RequestMapping("/eduService/frontCourse")

public class CourseFrontController {

    @Autowired
    private EduCourseService courseService;
    @Autowired
    private EduChapterService chapterService;
    @Autowired
    private OrderClient orderClient;

    @ApiOperation("条件查询分页查询课程")
    @PostMapping("/{page}/{limit}")
    public Result pageList(  @ApiParam(name = "page", value = "当前页码", required = true) @PathVariable Long page,
                             @ApiParam(name = "limit", value = "每页记录数", required = true) @PathVariable Long limit,
                             @ApiParam(name = "courseQuery", value = "查询对象", required = false) @RequestBody CourseFrontVo courseQuery) {
        Page<Course> pageParam = new Page<>(page, limit);
        System.out.println("查询对象为===》"+courseQuery);
        Map<String, Object> map = courseService.pageListWeb(pageParam, courseQuery);
        return  Result.ok().data("map",map);
    }

    @ApiOperation("课程详情的方法")
    @GetMapping("/getFrontCourseInfo/{courseID}")
    public Result getFrontCourseInfo(@ApiParam(name = "courseID", value = "课程详情对应的id", required = true)@PathVariable("courseID") String courseID
                                     , HttpServletRequest request){
        //根据课程id，编写sql语句查询课程信息
        System.out.println("请求开始");
        CourseWebVo courseWebVo=courseService.getBaseCourseInfo(courseID);

        //根据课程id查询章节和小节
        List<ChapterVo> chapterVoList = chapterService.getChapterVideoByCourseId(courseID);


        //远程调用，判断课程是否被购买
        boolean buyCourse = orderClient.isBuyCourse(JwtUtils.getMemberIdByJwtToken(request), courseID);
        System.out.println("判断课程是否被购买："+buyCourse);

        return Result.ok().data("courseWebVo",courseWebVo).data("chapterVideoList",chapterVoList).data("isbuy",buyCourse);
    }

    @ApiOperation("根据课程id查询课程信息")
    @PostMapping("getCourseInfoOder/{id}")
    public CourseWebVoOrder getCourseInfoOder(@PathVariable("id") String id){
        CourseWebVo baseCourseInfo = courseService.getBaseCourseInfo(id);
        CourseWebVoOrder courseWebVoOrder = new CourseWebVoOrder();
        BeanUtils.copyProperties(baseCourseInfo,courseWebVoOrder);
        return courseWebVoOrder;
    }
}
